<?php
    /* functions */
    function serviceFmc($data)
    {
        $username = "hdc";
        $password = "cdh2012";
        $service = 124;
        $cmdcode = "";
        
        $url = "http://123.30.187.131:8182/client/fmc.php";
        $url .= "?user=" . $username;
        $url .= "&pass=" . $password;
        $url .= "&service=" . $service;
        $url .= "&phone=" . $data["sender"];
        $url .= "&message=" . urlencode($data["content"]);
        $url .= "&shortcode=" . $data["serviceCode"];
        $url .= "&cmdcode=" . $cmdcode;
        $url .= "&txtid=" . $data["transactionId"];
        
        $xmlData = @simplexml_load_file($url, "SimpleXMLElement", LIBXML_NOCDATA);

        $result = array();
        $result["dk_status"] = "0";
        $result["dk_content"] = "";
            
        if($xmlData !== false){
            foreach($xmlData->children() as $child){                            
                if($child->getName()=="dk_status"){
                    $result["dk_status"] = strval($child);    
                } else if($child->getName()=="dk_content"){                 
                    $result["dk_content"] = strval($child);
                }
            }        
        }
        
        $response["dk_status"] = $result["dk_status"] == "1000" ? 1 : 0;
        $response["dk_content"] = $result["dk_status"] == "1000" ? $result["dk_content"] : "Yeu cau chua duoc xu ly. Chung toi se xu ly trong thoi gian som nhat";
                    
        return $response;
    }
    
    function serviceFmcFungame($data)
    {
        $username = "hdc";
        $password = "cdh2012";
        $service = 124;
        $cmdcode = "";
        
        $url = "http://123.30.187.131:8182/client/fmc.php";
        $url .= "?user=" . $username;
        $url .= "&pass=" . $password;
        $url .= "&service=" . $service;
        $url .= "&phone=" . $data["sender"];
        $url .= "&message=" . urlencode($data["new_content"]);
        $url .= "&shortcode=" . $data["serviceCode"];
        $url .= "&cmdcode=" . $cmdcode;
        $url .= "&txtid=" . $data["transactionId"];
        
        $xmlData = @simplexml_load_file($url, "SimpleXMLElement", LIBXML_NOCDATA);

        $result = array();
        $result["dk_status"] = "0";
        $result["dk_content"] = "";
            
        if($xmlData !== false){
            foreach($xmlData->children() as $child){                            
                if($child->getName()=="dk_status"){
                    $result["dk_status"] = strval($child);    
                } else if($child->getName()=="dk_content"){                 
                    $result["dk_content"] = strval($child);
                }
            }        
        }
        
        $response["dk_status"] = $result["dk_status"] == "1000" ? 1 : 0;
        $response["dk_content"] = $result["dk_status"] == "1000" ? $result["dk_content"] : "Yeu cau chua duoc xu ly. Chung toi se xu ly trong thoi gian som nhat";
                    
        return $response;
    }   
    
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);        
        $code = !empty($match[2]) ? $match[2] : "";
        $userId = !empty($match[4]) ? $match[4] : 0;
        
        
        /* 
        * Kiểm tra user có tồn tại không
        * Không tồn tại thì trả về 0
        */
        
        $user = getUserById($userId);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["code"] = $code;
        
        return $result;
    }
    
    function buildSyntaxFungame($content, $regx)
    {
        preg_match($regx, $content, $match);        
        $code = !empty($match[2]) ? $match[2] : "";
        $codeFungame = !empty($match[3]) ? $match[3] : 0;
        $endString = !empty($match[4]) ? $match[4] : "";
        
        
        /* 
        * Kiểm tra user có tồn tại không
        * Không tồn tại thì trả về 0
        */
        
        $user = getUserFungameByCode($codeFungame);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["userId"] : 0;
        $result["username"] = !empty($user) ? $user["username"] : "";
        $result["code"] = $code;
        $result["endString"] = $endString;
        
        return $result;
    }
    
    function getUserFungameByCode($code)
    {
        $sql = "SELECT userId, username FROM c_fungame WHERE `code`='" . mysql_escape_string($code) . "'";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;
    }
    
    /*
    * processing
    */
    
    function processing($data)
    {        
        $regx_phongthan = "/^TVFM(\s+)(UAFS|UDFS|ULFS|UWFS|UMFS)(\s+)(\d+)$/si";
        $regx_kiemkhach = "/^TVFM(\s+)(RSM|UMSM|UP)(\s+)(\d+)$/si";        
        $regx_cbr = "/^TVFM(\s+)(CBRUP|CBRRE)(\s+)(\d+)$/si";
        $regx_pipo = "/^TVFM(\s+)(PP|PPNAP)(\s+)(\d+)$/si";
        $regx_robo = "/^TVBC(\s+)(REG|UPG)(\s+)(\d+)/si";
        $regx_tank = "/^TVFM(\s+)(TANK|UPGRADE)(\s+)(\d+)$/si";
        $regx_fungame = "/^TVFM(\s+)(FUNGAME)(\d+)(.*)/si";        
        $regx_mobrunner = "/^TVFM(\s+)(MOBR|MOBU)(\s+)(\d+)(.*)/si";        
        $regx_tieuhoangde = "/^TVFM(\s+)(RMER|UMER)(\s+)(\d+)(.*)/si";        
        
        $data_reg = array();
        if(
            preg_match($regx_phongthan, $data["content"]) 
            || preg_match($regx_kiemkhach, $data["content"]) 
            || preg_match($regx_cbr, $data["content"])
            || preg_match($regx_pipo, $data["content"])
            || preg_match($regx_robo, $data["content"])
            || preg_match($regx_tank, $data["content"])
            || preg_match($regx_fungame, $data["content"])
            || preg_match($regx_mobrunner, $data["content"])
            || preg_match($regx_tieuhoangde, $data["content"])
        ){
            
            if(preg_match($regx_phongthan, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntax($data["content"], $regx_phongthan);
                if(strtoupper($syntax["code"])=="UAFS" || strtoupper($syntax["code"])=="UDFS"){
                    $list_service_code = array("6722");                    
                    $data["id"] = 153; //ID game
                } else if(strtoupper($syntax["code"])=="ULFS" || $syntax["code"]=="UWFS"){
                    $list_service_code = array("6622");                        
                    $data["id"] = 153; //ID game
                } else if(strtoupper($syntax["code"])=="UMFS"){
                    $list_service_code = array("6522");                        
                    $data["id"] = 153; //ID game
                } 
            } else if(preg_match($regx_kiemkhach, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntax($data["content"], $regx_kiemkhach);
                if(strtoupper($syntax["code"])=="RSM"){
                    $list_service_code = array("6622");                    
                    $data["id"] = 154; //ID game
                } else if(strtoupper($syntax["code"])=="UMSM"){
                    $list_service_code = array("6522");                        
                    $data["id"] = 154; //ID game
                } else if(strtoupper($syntax["code"])=="UP"){
                    $list_service_code = array("6322");                        
                    $data["id"] = 154; //ID game
                } 
            } else if(preg_match($regx_cbr, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntax($data["content"], $regx_cbr);
                if(strtoupper($syntax["code"])=="CBRUP"){
                    $list_service_code = array("6522");                    
                    $data["id"] = 157; //ID game
                } else if(strtoupper($syntax["code"])=="CBRRE"){
                    $list_service_code = array("6722");                        
                    $data["id"] = 157; //ID game
                } 
            } else if(preg_match($regx_pipo, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntax($data["content"], $regx_pipo);
                if(strtoupper($syntax["code"])=="PP"){
                    $list_service_code = array("6722");                    
                    $data["id"] = 161; //ID game
                } else if(strtoupper($syntax["code"])=="PPNAP"){
                    $list_service_code = array("6522");                        
                    $data["id"] = 161; //ID game
                } 
            } else if(preg_match($regx_robo, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntax($data["content"], $regx_robo);
                if(strtoupper($syntax["code"])=="REG"){
                    $list_service_code = array("6722");                    
                    $data["id"] = 155; //ID game
                } else if(strtoupper($syntax["code"])=="UPG"){
                    $list_service_code = array("6522");                        
                    $data["id"] = 155; //ID game
                } 
            } else if(preg_match($regx_tank, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntax($data["content"], $regx_tank);
                if(strtoupper($syntax["code"])=="TANK"){
                    $list_service_code = array("6722");                    
                    $data["id"] = 163; //ID game
                } else if(strtoupper($syntax["code"])=="UPGRADE"){
                    $list_service_code = array("6522");                        
                    $data["id"] = 163; //ID game
                } 
            } else if(preg_match($regx_fungame, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntaxFungame($data["content"], $regx_fungame);
                
                if(strtoupper($syntax["code"])=="FUNGAME"){                
                    $list_service_code = array("6022", "6522", "6622", "6722");                    
                    $data["id"] = 165; //ID game
                    $data_reg["username"] = $syntax["username"];
                    $data_reg["game_user"] = $syntax["endString"];
                } 
            } else if(preg_match($regx_mobrunner, $data["content"])){
                 
                /* Đăng ký user */
                $syntax = buildSyntax($data["content"], $regx_mobrunner);
                if(strtoupper($syntax["code"])=="MOBR"){
                    $list_service_code = array("6722");                    
                } else if(strtoupper($syntax["code"])=="MOBU"){
                    $list_service_code = array("6522");                                            
                } 
                $data["id"] = 180; //ID game
            } else if(preg_match($regx_tieuhoangde, $data["content"])){
                 
                /* Đăng ký user */
                $syntax = buildSyntax($data["content"], $regx_tieuhoangde);
                if(strtoupper($syntax["code"])=="RMER"){
                    $list_service_code = array("6722");                    
                } else if(strtoupper($syntax["code"])=="UMER"){
                    $list_service_code = array("6522");                                            
                } 
                $data["id"] = 179; //ID game
            } 
            
            $data["userId"] = $syntax["userId"];
            
            if(in_array($data["serviceCode"], $list_service_code)){
                if(preg_match($regx_fungame, $data["content"])){                     
                    $data["new_content"] = str_replace("FUNGAME", "FUNGAME ", $data["content"]);
                    $result = serviceFmcFungame($data);
                } else {
                    $result = serviceFmc($data);    
                }
                
                $data["status"] = $result["dk_status"];
                $response["status"] = $result["dk_status"];
                $response["msg"] = $result["dk_content"];
                
            } else {
                
                $response["status"] = 3;                        
                $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                
                $data["status"] = 0;                 
            }               
                
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
            
            $data["userId"] = 0;                
            $data["status"] = 0;    
            
            $data["id"] = 0; //ID game
        }
        
        /* log sms */        
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 57; //ID partner FMC    
        $data["type"] = 1; //Thể loại game   
        
        $data["output"] = $response["status"] . "|" . $data["transactionId"] . "|" . $response["msg"];    
        
        /* tỷ lệ ăn chia */
        $percent = getPercent($data["id"]);
        
        $data["percent_wapmaster"] = isset($percent["percent_wapmaster"]) ? $percent["percent_wapmaster"] : 0;
        $data["percent_partner"] = isset($percent["percent_partner"]) ? $percent["percent_partner"] : 0;
        $data["action"] = "reg";
        
        $result = InsertSmsLog($data);        
        
        if($data["id"]==165){
            $data_reg["userId"] = $data["userId"];    
            $data_reg["gameId"] = $data["id"];    
            
            /* Check xem user da ton tai chua */
            $result = CheckGameUser($data_reg);
            if($result==0){
                $result = InsertGameUser($data_reg);    
            }            
        }
        return $response;
    }
?>